<%--
  Created by IntelliJ IDEA.
  User: 92329
  Date: 2018/4/19
  Time: 14:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="common.jspf"%>
<html>
<head>
    <title>Title</title>
    <script>
        $(function () {
            load();
        });
        function load(){
            $.post("${hkxyPath}/downloads/ftp-list-json",function (result) {
                if(result.code==1){
                    var list=result.data;
                    var html='';
                    $.each(list,function (i,val) {
                        html+="<tr id='tr"+val.id+"'>" +
                            "<td><span>"+val.name+"</span></td>" +
                            "<td><a target='_blank' href='"+val.url+"'>"+val.url+"</a></td>" +
                            "<td>" +
                                "<button class='btn btn-primary btn-sm' onclick='modifyFtp("+JSON.stringify(val)+")'>修改</button>" +
                                "<button class='btn btn-primary btn-sm' onclick='deleteFTP("+val.id+")'>删除</button>" +
                            "</td>" +
                            "</tr>";
                    });
                    $("#ftpList").html(html);
                }else{
                    tips(0);
                    console.log(result.message);
                }
            });
        }
        function addFTP() {
            var html="<tr>"+
                "<td><input id='ftpname' class='form-control ' name='name' type='text'  placeholder='输入FTP链接名称' /></td>" +
                "<td><input id='ftpurl' class='form-control' name='url' type='url'  placeholder='输入FTP链接，例如：ftp://ftp.zzti.edu.cn/'/></td>" +
                "<td>" +
                    "<button class='btn btn-primary btn-sm' onclick='submitFTP()'>保存</button>" +
                "</td>"+
                "</tr>"
            $("#ftpList").append(html);
        }
        function submitFTP() {
            var name=$("#ftpname").val();
            var url=$("#ftpurl").val();
            if(name==""||url==""){
                alert("标题或链接都不允许为空！！！");
            }else{
                $.post("${hkxyPath}/downloads/ftp-add",{name:name,url:url},function (result) {
                    if(result.code==1){
                        tips(1);
                        load();
                    }else{
                        tips(0);
                        console.log(result.message);
                    }
                })
            }
        }
        function modifyFtp(ftp) {
            var html=
                "<td><input id='ftpname1' class='form-control ' name='name' type='text' value='"+ftp.name+"' placeholder='输入FTP链接名称' /></td>" +
                "<td><input id='ftpurl1' class='form-control' name='url' type='url' value='"+ftp.url+"' placeholder='输入FTP链接，例如：ftp://ftp.zzti.edu.cn/'/></td>" +
                "<td>" +
                    "<button class='btn btn-primary btn-sm' onclick='domodifyFTP("+ftp.id+")'>保存</button>" +
                "</td>"
            var trid="#tr"+ftp.id;
            $(trid).html(html);

        }
        function domodifyFTP(id) {
            var name=$("#ftpname1").val();
            var url=$("#ftpurl1").val();
            if(name==""||url==""){
                alert("标题或链接都不允许为空！！！");
            }else{
                $.post("${hkxyPath}/downloads/ftp-modify",{id:id,name:name,url:url},function (result) {
                    if(result.code==1){
                        tips(1);
                        load();
                    }else{
                        tips(0);
                        console.log(result.message);
                    }
                })
            }
        }
        function deleteFTP(id) {
            var c=confirm("确认删除？？？\n删除后无法恢复！！！");
            if(c){
                $.post("${hkxyPath}/downloads/ftp-delete",{id:id},function (result) {
                    if(result.code==1){
                        tips(1);
                        load();
                    }else{
                        tips(0);
                        console.log(result.message);
                    }
                })
            }
        }
        function preview(url) {
            window.open(url);
        }
    </script>
    <style>

    </style>
</head>
<body>
<!-- tips -->
<%@include file="tips.jspf"%>
<!-- Header -->
<div>
    <h2 class="header-bottom-my1">FTP链接配置</h2>
</div>
<!-- Filter -->
<div class="padding-5">
    <button class='btn btn-primary' onclick="addFTP()">增加资源</button>
</div>
<!-- List -->
<div id="ftpContent" class="padding-5 well" style="background-color:white;">
    <table class="table table-hover" style="padding-top: 10px;padding-bottom: 10px">
        <thead>
        <tr>
            <th class="col-md-4">名称</th>
            <th class="col-md-6">链接</th>
            <th class="col-md-2">操作</th>
        </tr>
        </thead>
        <tbody class="" id="ftpList">



        </tbody>
    </table>
</div>

</body>
</html>
